import { defineStore } from 'pinia'
import { getStorage, setStorage } from '@/utils/storage'
import { HISTORY_LIST } from '@/assets/js/constant'

export const useSearchStore = defineStore('search', {
  state: () => ({
    history: getStorage(HISTORY_LIST) || []
  }),
  actions: {
    setHistory(data) {
      const index = this.history.findIndex((item) => item === data)
      if (index !== -1) {
        this.history.splice(index, 1)
      }
      this.history.unshift(data)
      setStorage(HISTORY_LIST, this.history)
    },
    clearHistory(data) {
      const index = this.history.findIndex((item) => item === data)
      this.history.splice(index, 1)
      setStorage(HISTORY_LIST, this.history)
    },
    clearAllHistory() {
      this.history = []
      setStorage(HISTORY_LIST, this.history)
    }
  },
  getters: {}
})
